package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.vo.CourseandclazzQueryCriteria;
import me.zhengjie.modules.system.domain.vo.NameAndTime;
import me.zhengjie.modules.system.domain.vo.StudentqaQueryCriteria;
import me.zhengjie.modules.system.service.*;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "交流互动")
@RequestMapping("/api/communicate")
public class CommunicateController {

    private final CourseService courseService;

    private final CourseandclazzService courseandclazzService;

    private final ClazzandstudentService clazzandstudentService;

    private final StudentqaService studentqaService;

    private final CourseandteachService courseandteachService;

    private final TeachService teachService;

    private final StudentqaresponseService studentqaresponseService;

    private final StudentService studentService;

    private final UserService userService;

    @GetMapping
    @Log("查询学生提问")
    @ApiOperation("查询学生提问")
    public ResponseEntity<PageResult<Studentqa>> queryCommunicate(@Param("NameAndTime") NameAndTime nameAndTime, Page<Object> page){
//        return new ResponseEntity<>(courseandclazzService.queryAll(criteria,page), HttpStatus.OK);
        //获取教师用户绑定课程
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("createUserId", SecurityUtils.getCurrentUserId());
        List<Course> courseList =  courseService.list(queryWrapper);
        List<Integer> courseIds = new ArrayList<>();

        for(Course course:courseList){
            courseIds.add(course.getId());
        }
        //获取课程绑定教材
        QueryWrapper queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.in("courseId",courseIds);
        List<Courseandteach> courseandteachList = new ArrayList<>();
        List<Integer> teachIds = new ArrayList<>();
        List<Teach> teachList = new ArrayList<>();
        if(courseIds.size()!=0){
            courseandteachList = courseandteachService.list(queryWrapper4);
            for(Courseandteach courseandteach:courseandteachList){
                teachIds.add(Math.toIntExact(courseandteach.getTeachid()));
            }
            QueryWrapper queryWrapper5 = new QueryWrapper<>();
            if(nameAndTime.getName()!=null){
                queryWrapper5.like("name",nameAndTime.getName());
            }

            queryWrapper5.in("id",teachIds);
            teachList = teachService.list(queryWrapper5);
        }



        //获取课程绑定邦班级
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("courseId",courseIds);
        List<Courseandclazz> courseandclazzList = new ArrayList<>();
        List<Integer> clazzIds = new ArrayList<>();
        if(courseIds.size()!=0){

            courseandclazzList = courseandclazzService.list(queryWrapper1);

            for(Courseandclazz courseandclazz:courseandclazzList){
                clazzIds.add(Math.toIntExact(courseandclazz.getClazzId()));
            }
        }


        //获取班级绑定学生
        QueryWrapper queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("clazzId",clazzIds);
        List<Clazzandstudent> clazzandstudentList= new ArrayList<>();
        List<Integer> studentUserIds = new ArrayList<>();

        if(clazzIds.size()!=0){

            clazzandstudentList = clazzandstudentService.list(queryWrapper2);
            for(Clazzandstudent clazzandstudent:clazzandstudentList){
                studentUserIds.add(Math.toIntExact(clazzandstudent.getUserId()));
            }
        }

        //获取学生列表
        List<Student> studentList = new ArrayList<>();
        if(studentUserIds.size()!=0){
            QueryWrapper queryWrapper7 = new QueryWrapper<>();
            queryWrapper7.in("userId",studentUserIds);
            studentList = studentService.list(queryWrapper7);
        }
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("studentQAId",studentqaIds);
//
//        queryWrapper.eq("createUserId",SecurityUtils.getCurrentUserId());
//        List<Studentqaresponse> studentqaresponseList = studentqaresponseMapper.selectList(queryWrapper);
        //获取学生回复
        QueryWrapper queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.in("createUserId",studentUserIds);
        queryWrapper3.ge("createTime",nameAndTime.getStartTime());
        queryWrapper3.lt("createTime",nameAndTime.getEndTime());
        queryWrapper3.in("teachId",teachIds);

        PageResult<Studentqa> studentqaList = null;
        List<Integer> studentqaIds = new ArrayList<>();

        if(studentUserIds.size() != 0){
            StudentqaQueryCriteria studentqaQueryCriteria = new StudentqaQueryCriteria();
            studentqaQueryCriteria.setTeachIds(teachIds);
            studentqaQueryCriteria.setUserIds(studentUserIds);
            studentqaQueryCriteria.setStartTime(nameAndTime.getStartTime());
            studentqaQueryCriteria.setEndTime(nameAndTime.getEndTime());

            studentqaList = studentqaService.queryAll(studentqaQueryCriteria,page);
            for(Studentqa studentqa:studentqaList.getContent()){
                studentqaIds.add(studentqa.getId());
                for(Teach teach:teachList){
                    if(Math.toIntExact(studentqa.getTeachId()) == teach.getId()){
                        studentqa.setTeachName(teach.getName());

                    }
                }
                for(Student student:studentList){
                    if(studentqa.getCreateUserId() == Math.toIntExact(student.getUserid())){
                        studentqa.setCreateName(student.getUsername());
                    }
                }
            }
            QueryWrapper queryWrapper6 = new QueryWrapper<>();
            queryWrapper6.in("studentQAId",studentqaIds);
            List<Studentqaresponse> studentqaresponseList = studentqaresponseService.list(queryWrapper6);

            List<Integer> allUserIds = new ArrayList<>();
            for(Studentqa studentqa:studentqaList.getContent()){
                List<Studentqaresponse> studentqaresponseList1 = new ArrayList<>();
                allUserIds.add(studentqa.getCreateUserId());
                for(Studentqaresponse studentqaresponse:studentqaresponseList){
                    if(studentqa.getId() == studentqaresponse.getStudentQAId()){
                        studentqaresponseList1.add(studentqaresponse);
                        allUserIds.add(studentqaresponse.getCreateUserId());
                    }
                }
                studentqa.setStudentqaresponseList(studentqaresponseList1);
            }
            QueryWrapper queryWrapper5 = new QueryWrapper<>();
            queryWrapper5.in("user_id",allUserIds);
            List<User> userList = userService.list(queryWrapper5);
            for(Studentqa studentqa:studentqaList.getContent()){

                for(User user:userList){
                    if(studentqa.getCreateUserId() == Math.toIntExact(user.getId())){
                        studentqa.setIcon(user.getAvatarPath());
                        studentqa.setCreateName(user.getUsername());
                    }
                    for(Studentqaresponse studentqaresponse:studentqa.getStudentqaresponseList()){
                        if(Math.toIntExact(user.getId()) == studentqaresponse.getCreateUserId()){
                            studentqaresponse.setIcon(user.getAvatarPath());
                            studentqaresponse.setCreateName(user.getUsername());
                        }
                    }
                }


            }
        }


        return new ResponseEntity<>(studentqaList,HttpStatus.OK);
    }

    @GetMapping("/queryStudentqa")
    @Log("查询学生提问详情")
    @ApiOperation("查询学生提问详情")
    public ResponseEntity<Studentqa> queryStudentqa(@Param("id") Integer id){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Studentqa studentqa = studentqaService.getOne(queryWrapper);
//
        QueryWrapper queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.eq("studentQAId",id);
        List<Studentqaresponse> studentqaresponseList = studentqaresponseService.list(queryWrapper6);
        studentqa.setStudentqaresponseList(studentqaresponseList);

        List<Integer> allUserIds = new ArrayList<>();
        allUserIds.add(studentqa.getCreateUserId());
        for(Studentqaresponse studentqaresponse:studentqa.getStudentqaresponseList()){
            allUserIds.add(studentqaresponse.getCreateUserId());
        }
        QueryWrapper queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.in("user_id",allUserIds);
        List<User> userList = userService.list(queryWrapper5);
        for(User user:userList){
            if(Math.toIntExact(user.getId()) == studentqa.getCreateUserId()){
                studentqa.setIcon(user.getAvatarPath());
                studentqa.setCreateName(user.getUsername());
            }
            for(Studentqaresponse studentqaresponse:studentqa.getStudentqaresponseList()){

                if(Math.toIntExact(user.getId()) == studentqaresponse.getCreateUserId()){
                    studentqaresponse.setCreateName(user.getUsername());
                    studentqaresponse.setIcon(user.getAvatarPath());
                }
            }
        }
        return new ResponseEntity<>(studentqa,HttpStatus.OK);
    }
}
